package com.aquarium.ui.controller;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.aquarium.R;
import com.aquarium.config.DosingPumpData;
import com.aquarium.config.ModuleData;
import com.aquarium.data.ModuleSettingDataBase;
import com.aquarium.module.ModuleControlService;

public class DosingPumpTimePicker extends ModuleTimePicker {

	private CheckBox mPump1CkBox;
	private CheckBox mPump2CkBox;
	private CheckBox mPump3CkBox;
	private CheckBox mPump4CkBox;
	private EditText mPump1DosingEditText;
	private EditText mPump2DosingEditText;
	private EditText mPump3DosingEditText;
	private EditText mPump4DosingEditText;

	private int mPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dosing_pump_time_picker);
		init();
	}

	@Override
	public void init() {
		super.init();
		if (mIntentAction.equals(ControllerSetting.ACTION_MODIFY_SETTING)) {
			mPosition = getIntent().getIntExtra(
					ControllerSetting.INTENT_EXTRA_LISTVIEW_CLICKED_POSITION, -1);
			if (mPosition != -1) {
				DosingPumpData data = mModuleSettingDataBase
						.queryDosingPumpData(mModuleSettingDataBase.getCursor(
								ModuleSettingDataBase.DOSING_PUMP_TABLE,
								mPosition));
				setWidgetValue(data);
			}
		}
		setDosingInputMax(255);
	}

	@Override
	public void setWidgetValue(ModuleData data) {
		super.setWidgetValue(data);
		mPump1CkBox.setChecked(((DosingPumpData) data).isPump1On);
		mPump2CkBox.setChecked(((DosingPumpData) data).isPump2On);
		mPump3CkBox.setChecked(((DosingPumpData) data).isPump3On);
		mPump4CkBox.setChecked(((DosingPumpData) data).isPump4On);
		mPump1DosingEditText.setText(String
				.valueOf(((DosingPumpData) data).pump1Dosing));
		mPump2DosingEditText.setText(String
				.valueOf(((DosingPumpData) data).pump2Dosing));
		mPump3DosingEditText.setText(String
				.valueOf(((DosingPumpData) data).pump3Dosing));
		mPump4DosingEditText.setText(String
				.valueOf(((DosingPumpData) data).pump4Dosing));
	}

	@Override
	public void findViews() {
		super.findViews();
		mPump1CkBox = (CheckBox) findViewById(R.id.pump1_ckbox);
		mPump2CkBox = (CheckBox) findViewById(R.id.pump2_ckbox);
		mPump3CkBox = (CheckBox) findViewById(R.id.pump3_ckbox);
		mPump4CkBox = (CheckBox) findViewById(R.id.pump4_ckbox);
		mPump1DosingEditText = (EditText) findViewById(R.id.pump1_edtx);
		mPump2DosingEditText = (EditText) findViewById(R.id.pump2_edtx);
		mPump3DosingEditText = (EditText) findViewById(R.id.pump3_edtx);
		mPump4DosingEditText = (EditText) findViewById(R.id.pump4_edtx);
	}

	@Override
	public void setListeners() {
		super.setListeners();
		mOkButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				DosingPumpData data = new DosingPumpData();
				setModuleData(data);
				data.isPump1On = mPump1CkBox.isChecked();
				data.isPump2On = mPump2CkBox.isChecked();
				data.isPump3On = mPump3CkBox.isChecked();
				data.isPump4On = mPump4CkBox.isChecked();
				if ("".equals(mPump1DosingEditText.getText().toString())) {
					data.pump1Dosing = 0;
				} else {
					data.pump1Dosing = Integer.valueOf(mPump1DosingEditText
							.getText().toString());
				}
				if ("".equals(mPump2DosingEditText.getText().toString())) {
					data.pump2Dosing = 0;
				} else {
					data.pump2Dosing = Integer.valueOf(mPump2DosingEditText
							.getText().toString());
				}
				if ("".equals(mPump3DosingEditText.getText().toString())) {
					data.pump3Dosing = 0;
				} else {
					data.pump3Dosing = Integer.valueOf(mPump3DosingEditText
							.getText().toString());
				}
				if ("".equals(mPump4DosingEditText.getText().toString())) {
					data.pump4Dosing = 0;
				} else {
					data.pump4Dosing = Integer.valueOf(mPump4DosingEditText
							.getText().toString());
				}

				if (mIntentAction.equals(ControllerSetting.ACTION_ADD_SETTING)) {
					mModuleSettingDataBase.insertDosingPumpData(data);
				} else if (mIntentAction
						.equals(ControllerSetting.ACTION_MODIFY_SETTING)) {
					mModuleSettingDataBase
							.updateDosingPunpData(mPosition, data);
				}
				Intent intent = new Intent(
						ModuleControlService.ACTION_UPDATE_DOSING_PUMP_DATA);
				sendBroadcast(intent);
				finish();
			}
		});
		mCancelButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void setDosingInputMax(int maxVolumn) {
		TextWatcher textWatcher1 = new DosingInputChangeWatcher(
				mPump1DosingEditText, 255);
		TextWatcher textWatcher2 = new DosingInputChangeWatcher(
				mPump2DosingEditText, 255);
		TextWatcher textWatcher3 = new DosingInputChangeWatcher(
				mPump3DosingEditText, 255);
		TextWatcher textWatcher4 = new DosingInputChangeWatcher(
				mPump4DosingEditText, 255);

		mPump1DosingEditText.addTextChangedListener(textWatcher1);
		mPump2DosingEditText.addTextChangedListener(textWatcher2);
		mPump3DosingEditText.addTextChangedListener(textWatcher3);
		mPump4DosingEditText.addTextChangedListener(textWatcher4);
	}

	public class DosingInputChangeWatcher implements TextWatcher {

		private EditText editText;
		private int max;

		public DosingInputChangeWatcher(EditText editText, int max) {
			this.editText = editText;
			this.max = max;
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (s.toString().equals("")) {
				return;
			}
			int value = Integer.parseInt(s.toString());
			if (value > max) {
				editText.setText(String.valueOf(max));
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub

		}

	}
}
